// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import { AdManager, ReportType } from "../../ads/AdManager";
import { EventManager } from "../../manager/EventManager";
import LevelBase from "../../LevelBase";
import LevelPre_18 from "./LevelPre_18";
import { EVENT_NAME_ENUM } from "../../base/EnumIndex";
import { LevelManager } from "../../manager/LevelManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class GameMusic extends cc.Component {

    // /**牌总结点 */
    // @property(cc.Node)
    // paiNode: cc.Node = null;

    // @property(cc.Node)
    // cardContent: cc.Node = null;

    // @property(cc.Node)
    // cardItem: cc.Node = null;

    // /**当前难度 */
    // @property(cc.Label)
    // lbNow: cc.Label = null;

    /**答案 */
    @property(cc.Node)
    keyBtn: cc.Node = null;

    private clicks = [];

    private keyIdx = -1;
    private musicId = null;

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        EventManager.ins.on(EVENT_NAME_ENUM.FINISH_INTER_TWEEN, this.finish, this);
    }
    onDestroy() {
        EventManager.ins.off(EVENT_NAME_ENUM.FINISH_INTER_TWEEN, this.finish, this);
    }
    finish(finishStr: any) {
        if (typeof finishStr != "string") {
            return;
        }
        let level18 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren(LevelPre_18);
        // if (finishStr.includes("initCards_73")) {
        //     this.paiNode.active = true;
        //     this.cardContent.removeAllChildren();
        //     this.selectItem = null;
        //     let idx = 1;
        //     let fun = () => {
        //         for (let item of musicCard) {
        //             if (item.id == idx) {
        //                 let card = cc.instantiate(this.cardItem);
        //                 card.getChildByName("lbDegree").getComponent(cc.Label).string = item.degree;
        //                 this.cardContent.addChild(card);
        //                 if (this.clicks.indexOf(item.id) != -1) {
        //                     card.getChildByName("lbDegree").active = true;
        //                     card.getChildByName("img").active = true;
        //                 }
        //                 card.on("click", () => {
        //                     if (this.clicks.indexOf(item.id) != -1) {
        //                         return;
        //                     }
        //                     this.selectItem = item;
        //                     this.clicks.push(item.id);
        //                     cc.tween(card).to(0.5, {
        //                         scaleX: 0
        //                     }).call(() => {
        //                         card.getChildByName("lbDegree").active = true;
        //                         card.getChildByName("img").active = true;
        //                     }).to(0.5, {
        //                         scaleX: 1
        //                     }).call(() => {
        //                         this.lbNow.node.active = true;
        //                         this.lbNow.string = "当前播放歌曲：" + item.degree;
        //                         this.paiNode.active = false;
        //                         this.keyBtn.active = true;
        //                         let levelBase = cc.director.getScene().getChildByName("Canvas").getComponentInChildren(LevelBase);
        //                         levelBase.runNodeTween(levelBase.node, levelBase.getInterTwForName("music_" + item.id).interTween);

        //                     }).start();
        //                 })
        //             }
        //         }
        //         if (idx > musicCard.length) {
        //             return;
        //         }
        //         this.scheduleOnce(() => {
        //             idx++;
        //             fun();
        //         }, 0.2)
        //     }
        //     fun();
        // }
        let levelBase = cc.director.getScene().getChildByName("Canvas").getComponentInChildren(LevelBase);
        if (finishStr.includes("checkIsWin")) {
            this.keyBtn.active = false;
            if (this.clicks.length == level18.interJson.keyNum) {
                let iswin = level18.getIsWin();
                if (iswin) {
                    levelBase.runNodeTween(levelBase.node, levelBase.getInterTwForName("winAniGame").interTween);
                } else {
                    levelBase.runNodeTween(levelBase.node, levelBase.getInterTwForName("overAniGame").interTween);
                }
            } else {
                let isRight = finishStr.split("checkIsWin_")[1];
                if (isRight == "true") {//答对
                    levelBase.runNodeTween(levelBase.node, levelBase.getInterTwForName("answerRight").interTween);
                } else {
                    levelBase.runNodeTween(levelBase.node, levelBase.getInterTwForName("answerError").interTween);
                }
            }
        } else if (finishStr.includes("choseCard_")) {
            this.keyBtn.active = true;
            this.node.getChildByName("sayNode3").active = false;
            let id = Number(finishStr.split("choseCard_")[1]);
            this.musicId = id;
            this.clicks.push(id);
            this.keyIdx = level18.interJson.musicKeys[id - 1]
        }

    }

    start() {
        this.keyBtn.on("click", () => {
            AdManager.umengEvent(ReportType.CLICK_ADS, { tag: "广告开始", name: LevelManager.ins.curLvInfo.name, value: "查看答案" });
            AdManager.showVideoAd(() => {
                AdManager.umengEvent(ReportType.CLICK_ADS, { tag: "广告完成", name: LevelManager.ins.curLvInfo.name, value: "查看答案" });
                let lb: cc.Label = this.node.getChildByName("choseUI_" + this.musicId).getChildByName("ans" + this.musicId + "_" + this.keyIdx).children[0].getComponent(cc.Label)
                lb.node.color = cc.Color.GREEN;
            });
        })
    }

    // update (dt) {}
}
